[. . . ] Red Hat Enterprise Linux 4 Red Hat Enterprise Linux 4 Copyright © 1987, 1989, 1991-2004 Free Software Foundation, Inc. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1. 2 or any later version published by the Free Software Foundation; with the Invariant Sections being "GNU General Public License" and "Funding Free Software", the Front-Cover texts being (a) (see below), and with the Back-Cover Texts being (b) (see below). A copy of the license is included in the section entitled "GNU Free Documentation License". (a) The FSF's Front-Cover Text is: A GNU Manual (b) The FSF's Back-Cover Text is: You have freedom to copy and modify this GNU Manual, like GNU software. [. . . ] For example: #define SEARCH(value, array, target) do { __label__ found; typeof (target) _SEARCH_target = (target); typeof (*(array)) *_SEARCH_array = (array); int i, j; int value; for (i = 0; i max; i++) for (j = 0; j max; j++) if (_SEARCH_array[i][j] == _SEARCH_target) { (value) = i; goto found; } (value) = -1; found:; } while (0) \ \ \ \ \ \ \ \ \ \ \ \ \ This could also be written using a statement-expression: #define SEARCH(array, target) ({ __label__ found; typeof (target) _SEARCH_target = (target); typeof (*(array)) *_SEARCH_array = (array); int i, j; int value; max; i++) for (i = 0; i for (j = 0; j max; j++) if (_SEARCH_array[i][j] == _SEARCH_target) { value = i; goto found; } value = -1; found: value; }) \ \ \ \ \ \ \ \ \ \ \ \ \ \ Local label declarations also make the labels they declare visible to nested functions, if there are any. Labels as Values You can get the address of a label defined in the current function (or a containing function) with the unary operator &&. This value is a constant and can be used wherever a constant of that type is valid. Extensions to the C Language Family To use these values, you need to be able to jump to one. One way of using these constants is in initializing a static array that will serve as a jump table: static void *array[] = { &&foo, &&bar, &&hack }; Then you can select a label with indexing, like this: goto *array[i]; Note that this does not check whether the subscript is in bounds--array indexing in C never does that. Such an array of label values serves a purpose much like that of the switch statement. The switch statement is cleaner, so use that rather than an array unless the problem does not fit a switch statement very well. The labels within the interpreter function can be stored in the threaded code for super-fast dispatching. You may not use this mechanism to jump to code in a different function. The best way to avoid this is to store the label address only in automatic variables and never pass it as an argument. An alternate way to write the above example is static const int array[] = { &&foo - &&foo, &&bar - &&foo, &&hack - &&foo }; goto *(&&foo + array[i]); This is more friendly to code living in shared libraries, as it reduces the number of dynamic relocations that are needed, and by consequence, allows the data to be read-only. 6. 4. Nested Functions A nested function is a function defined inside another function. (Nested functions are not supported for GNU C++. ) The nested function's name is local to the block where it is defined. For example, here we define a nested function named square, and call it twice: foo (double a, double b) { double square (double z) { return z * z; } return square (a) + square (b); 1. The analogous feature in Fortran is called an assigned goto, but that name seems inappropriate in C, where one can do more than simply store label addresses in label variables. Chapter 6. Extensions to the C Language Family 135 } The nested function can access all the variables of the containing function that are visible at the point of its definition. For example, here we show a nested function which uses an inherited variable named offset: bar (int *array, int offset, int size) { int access (int *array, int index) { return array[index + offset]; } int i; /* . . . */ } Nested function definitions are permitted within functions in the places where variable definitions are allowed; that is, in any block, before the first statement in the block. It is possible to call the nested function from outside the scope of its name by storing its address or passing the address to another function: hack (int *array, int size) { void store (int index, int value) { array[index] = value; } intermediate (store, size); } Here, the function intermediate receives the address of store as an argument. If intermediate calls store, the arguments given to store are used to store into array. But this technique works only so long as the containing function (hack, in this example) does not exit. If you try to call the nested function through its address after the containing function has exited, all hell will break loose. [. . . ] So when you compare distributors, judge them partly by how much they give to free software development. To make this approach work, you must insist on numbers that you can compare, such as, "We will donate ten dollars to the Frobnitz project for each disk sold. " Don't be satisfied with a vague promise, such as "A portion of the profits are donated, " since it doesn't give a basis for comparison. Even a precise fraction "of the profits from this disk" is not very meaningful, since creative accounting and unrelated business decisions can greatly alter what fraction of the sales price counts as profit. If the price you pay is $50, ten percent of the profit is probably less than a dollar; it might be a few cents, or nothing at all. [. . . ]